/*
1、写一个主程序来上机设计并验证线性表顺序表示的所有操作(至少包含算法2.3、2.4、2.5)，并设计一个算法删除所有值大于min而且小于max的元素。
*/

#include <iostream>
using namespace std;
#define OK 0;
#define ERR -1;

const int MAX_SIZE = 100;
const int MAX_N = 10.0;
const int MIN_N = -10.0;

typedef struct{
    float *base;
    int length;
} Sqlist;


void showList(Sqlist* L){
    printf("size: %d\n========\n",L->length);
    for (int i = 0;i< L->length;i++){
        printf("No: %d --> %f\n",i,L->base[i]);
    }
}

int init(Sqlist *L){
    L->base = new float[MAX_SIZE];
    L->length = 0;
    for (int i = 0; i < MAX_SIZE; i++)
    {
        L->base[i] = 0;
    }
    return OK;
}

// 2.3 search
int getElem(Sqlist *L, float e){
    for (int i = 0; i < L->length; i++){
        if(e == L->base[i]) return i+1;
    }
    return OK;
}

// 2.4 insert
int insertElem(Sqlist *L, int index, float e){
    // judge the value
    if (e >= MAX_N || e <= MIN_N)
        return ERR;
    // judge the range
    if (L->length + 1 > MAX_SIZE)
        return ERR;

    // move elements
    float *arr = L->base;
    for (int i = L->length; i > index; i--)
    {
        arr[i] = arr[i-1];
    }
    arr[index] = e;
    L->length++;

    return OK;
}

// 2.5 delete
int deleteElem(Sqlist* L, int index){
    // judge the range
    if(index <0 && index > L->length-1) return ERR;

    float* arr = L->base;
    for (int i = index; i< L->length-1; i++){
        arr[i] = arr[i+1];
    }
    arr[L->length] = 0;
    L->length--;
    return OK;
}

// delete element in range
int deleteElementInRange(Sqlist* L,int min,int max){
    float* arr = L->base;
    float* newArr = new float[MAX_SIZE];
    int count = 0;
    for (int i=0; i< MAX_SIZE; i++){
        newArr[i] = 0;
        if(i < L->length){
            if(arr[i]<min || arr[i]>max){
                newArr[count] = arr[i];
                count++;
            }
        }else{
            continue;
        }
        
    }
    L->base = newArr;
    L->length = count;
    delete arr;
    return OK;
}

int main()
{
    Sqlist* list = new Sqlist;
    

    init(list);
    insertElem(list,0, 1.1);
    insertElem(list,1, 2.1);
    insertElem(list,2, 3.1);
    insertElem(list,3, 4.1);
    insertElem(list,4, 5.1);
    insertElem(list,5, 6.1);
    showList(list);

    deleteElem(list,5);
    deleteElem(list,0);
    deleteElementInRange(list,1,3);
    showList(list);
    return 0;
}